package com.banciyuan.bcywebview.biz.post.rule;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by squall on 2016/1/11.
 */
public class RuleHelper {

    private IRuleInter iRuleInter;

    private Context mContext;

    private RequestQueue mQueue;

    public RuleHelper(Context mContext, IRuleInter iRuleInter) {
        this.mContext = mContext;
        this.iRuleInter = iRuleInter;
        this.mQueue = VolleyQueue.getRquest(mContext);
    }

    public void judge(final String options) {
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        if (UserDataHelper.ifLogin(mContext))
            paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        final HashMap<String, String> params = HttpUtils.getData(paramsList);
        String url = HttpUtils.BASE_URL + options;
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject result = new JSONObject(s);
                    Object status = result.get(HttpUtils.UPLOAD_STATUS);
                    String st = status.toString();
                    if (st.equals("1")) {
                        iRuleInter.judgeTrue(result);
                    } else {
                        iRuleInter.judgeFalse(result);

                    }
                } catch (JSONException e) {
                    MyToast.show(mContext, mContext.getString(R.string.network_something_wrong));
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.show(mContext, mContext.getString(R.string.network_something_wrong));
            }
        };
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, error);
        mQueue.add(stringRequest);
    }

    public void verify(final String options) {
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        final HashMap<String, String> params = HttpUtils.getData(paramsList);
        String url = HttpUtils.BASE_URL +
                options;
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject result = new JSONObject(s);
                    Object status = result.get(HttpUtils.UPLOAD_STATUS);
                    String st = status.toString();
                    if (st.equals("1")) {
                        iRuleInter.changeSucc(result);
                    } else {
                        iRuleInter.changeFail(result);
                    }
                } catch (JSONException e) {
                    MyToast.show(mContext, mContext.getString(R.string.network_something_wrong));
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                MyToast.show(mContext, mContext.getString(R.string.network_something_wrong));
            }
        };
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, error);
        mQueue.add(stringRequest);
    }

}
